/**
 * @file setting_brightness.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SETTING_BRIGHTNESS != 0

/*********************
 *      DEFINES
 *********************/
setting_brightness_t settingbrightness;

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
HAL_INTENSITY_LEVEL setting_backlight_intensity_to_hal_intensity(uint8_t intensity)
{
    HAL_INTENSITY_LEVEL halintensity;

    switch((intensity % 2) + (intensity / 2)) {
        case 1:
            halintensity = HAL_INTENSITY_LEVEL_1;
            break;
        case 2:
            halintensity = HAL_INTENSITY_LEVEL_2;
            break;
        case 3:
            halintensity = HAL_INTENSITY_LEVEL_3;
            break;
        case 4:
            halintensity = HAL_INTENSITY_LEVEL_4;
            break;
        case 5:
            halintensity = HAL_INTENSITY_LEVEL_5;
            break;
        default:
            halintensity = HAL_INTENSITY_LEVEL_1;
            break;
    }

    return(halintensity);
}

uint8_t setting_get_backlight_intensity(void)
{
    return settingbrightness.backlightintensity;
}

void setting_set_backlight_intensity(uint8_t intensity, bool writenvm)
{
    uint8_t nvdata;
    HAL_INTENSITY_LEVEL halintensity;

    // update global variable
    settingbrightness.backlightintensity = intensity;

    // nv write
    if(writenvm) {
        nvdata = intensity;
        printf("%s write backlight to nvm, %d\n", __FUNCTION__, nvdata);
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, 2, 1, &nvdata);
    }

    // hal api
#if (USE_LCD_PANEL_ICNA3310_MIPI || USE_LCD_PANEL_RM69330_MIPI)
    Hal_Backlight_Intensity_Set_For_Oledpanel(intensity);
#else
    halintensity = setting_backlight_intensity_to_hal_intensity(intensity);
    Hal_Backlight_Intensity_Set(halintensity);
#endif
}

void setting_brightness_init(nv_watch_settings_t * nvsettings)
{
    HAL_INTENSITY_LEVEL halintensity;

    // init manage variable
    memset(&settingbrightness, 0, sizeof(setting_brightness_t));

    settingbrightness.backlightintensity = nvsettings->backlightintensity;
    settingbrightness.backlighttimeout = nvsettings->backlighttimeout;

    // hal api
#if (USE_LCD_PANEL_ICNA3310_MIPI || USE_LCD_PANEL_RM69330_MIPI)
     Hal_Backlight_Intensity_Set_For_Oledpanel(settingbrightness.backlightintensity);
#else
    halintensity = setting_backlight_intensity_to_hal_intensity(settingbrightness.backlightintensity);
    Hal_Backlight_Intensity_Set(halintensity);
#endif
}

lv_obj_t * setting_brightness_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    lv_obj_t * label;
    lv_obj_t * bar_content;
    lv_obj_t * button_content;

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SETTING_BRIGHTNESS;
        activity_ext.create = setting_brightness_create;
        activity_ext.prepare_destory = setting_brightness_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_BRIGHTNESS_ADJUST);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);
    lv_watch_set_free_num(label, 0);

    bar_content = setting_brightness_bar_content_create(obj);
    lv_obj_add_style(bar_content, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_align(bar_content, obj, LV_ALIGN_IN_BOTTOM_MID, 0, -(LV_VER_RES * 3 / 5));
    lv_watch_set_free_num(bar_content, 1);

    button_content = setting_brightness_button_content_create(obj);
    lv_obj_add_style(button_content, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_align(button_content, obj, LV_ALIGN_IN_BOTTOM_MID, 0, -(LV_VER_RES  / 4));
    lv_watch_set_free_num(button_content, 2);

    return obj;
}

lv_obj_t * setting_brightness_bar_content_create(lv_obj_t * par)
{
    lv_obj_t * content;
    lv_obj_t * img[10];
    uint8_t i;

    if(!par) {
        par = lv_scr_act();
    }

    content = lv_cont_create(par, NULL);
    lv_cont_set_fit(content, LV_FIT_TIGHT);
    lv_cont_set_layout(content, LV_LAYOUT_ROW_BOTTOM);
    LV_ASSERT_MEM(content);
    if(NULL == content) {
        return NULL;
    }

    lv_obj_set_click(content, false);
    lv_watch_obj_add_element(content);

    for(i = 0; i < 10; i++) {
        img[i] = lv_img_create(content, NULL);
        LV_ASSERT_MEM(img[i]);
        if(NULL == img[i]) {
            return NULL;
        }

        if(i < settingbrightness.backlightintensity) {
            lv_img_set_src(img[i], ICON_SUB_BOX_YELLOW);
        } else {
            lv_img_set_src(img[i], ICON_SUB_BOX_GRAY);
        }

        lv_watch_set_free_num(img[i], i);
        lv_watch_obj_add_element(img[i]);
    }

    return content;
}

lv_obj_t * setting_brightness_button_content_create(lv_obj_t * par)
{
    lv_obj_t * content;
    lv_obj_t * btn1;
    lv_obj_t * btn2;
    lv_obj_t * img;

    if(!par) {
        par = lv_scr_act();
    }

    content = lv_cont_create(par, NULL);
    lv_cont_set_fit(content, LV_FIT_TIGHT);
    lv_cont_set_layout(content, LV_LAYOUT_ROW_MID);
    LV_ASSERT_MEM(content);
    if(NULL == content) {
        return NULL;
    }

    lv_obj_set_click(content, true);
    lv_watch_obj_add_element(content);

    btn1 = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(btn1, LV_BTN_STATE_RELEASED, ICON_DECREASE);
    lv_imgbtn_set_src(btn1, LV_BTN_STATE_PRESSED, ICON_DECREASE);
    lv_obj_set_event_cb(btn1, setting_brightness_decrease_btn_action);
    lv_watch_obj_add_element(btn1);

    img = lv_img_create(content, NULL);
    LV_ASSERT_MEM(img);
    if(NULL == img) {
        return NULL;
    }
    lv_img_set_src(img, ICON_BRIGHTNESS);
    lv_watch_obj_add_element(img);


    btn2 = lv_imgbtn_create(content, NULL);
    lv_imgbtn_set_src(btn2, LV_BTN_STATE_RELEASED, ICON_INCREASE);
    lv_imgbtn_set_src(btn2, LV_BTN_STATE_PRESSED, ICON_INCREASE);
    lv_obj_set_event_cb(btn2, setting_brightness_increase_btn_action);
    lv_watch_obj_add_element(btn2);

    return content;
}

void setting_brightness_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;

    lv_watch_png_cache_all_free();

    lv_obj_t * obj = setting_brightness_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;
}


void setting_brightness_prepare_destory(lv_obj_t * activity_obj)
{
    setting_set_backlight_intensity(setting_get_backlight_intensity(), true);
    lv_watch_png_cache_all_free();
}

void setting_brightness_decrease_btn_action(lv_obj_t * btn, lv_event_t event)
{
    lv_obj_t * par;
    lv_obj_t * child = NULL;
    lv_obj_t * content;
    lv_obj_t * img = NULL;
    uint8_t i;
    uint8_t backlightintensity = settingbrightness.backlightintensity;

    if(LV_EVENT_PRESSED != event) return;

    content = lv_obj_get_parent(btn);
    par = lv_obj_get_parent(content);

    for(i = 0; i < 3; i++) {
        child = lv_obj_get_child(par, child);
        if(1 == lv_obj_get_user_data(child).user_num) {
            break;
        }
    }

    for(i = 0; i < 10; i++) {
        img = lv_obj_get_child(child, img);
        if(backlightintensity <= lv_obj_get_user_data(img).user_num) {
            continue;
        } else {
            lv_img_set_src(img, ICON_SUB_BOX_GRAY);
            backlightintensity--;
            setting_set_backlight_intensity(backlightintensity, false);
            break;
        }
    }
}

void setting_brightness_increase_btn_action(lv_obj_t * btn, lv_event_t event)
{
    lv_obj_t * par;
    lv_obj_t * child = NULL;
    lv_obj_t * content;
    lv_obj_t * img = NULL;
    uint8_t i;
    uint8_t backlightintensity = settingbrightness.backlightintensity;

    if(LV_EVENT_PRESSED != event) return;

    content = lv_obj_get_parent(btn);
    par = lv_obj_get_parent(content);

    for(i = 0; i < 3; i++) {
        child = lv_obj_get_child(par, child);
        if(1 == lv_obj_get_user_data(child).user_num) {
            break;
        }
    }

    for(i = 0; i < 10; i++) {
        img = lv_obj_get_child_back(child, img);
        if(backlightintensity > lv_obj_get_user_data(img).user_num) {
            continue;
        } else {
            lv_img_set_src(img, ICON_SUB_BOX_YELLOW);
            backlightintensity++;
            setting_set_backlight_intensity(backlightintensity, false);
            break;
        }
    }
}

uint8_t setting_get_backlight_timeout(void)
{
    if(settingbrightness.backlighttimeout != 15) {
        printf("error: backlighttimeout is over write %u\n", settingbrightness.backlighttimeout);
        settingbrightness.backlighttimeout = 15;
    }

    return settingbrightness.backlighttimeout;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

#endif /*USE_LV_WATCH_SETTING_BRIGHTNESS*/
